package com.ruoyi.house.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.house.domain.XlkjHouseSurvey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.house.mapper.XlkjHouseMapper;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;

/**
 * 房屋Service业务层处理
 *
 * @author wang
 * @date 2022-01-11
 */
@Service
public class XlkjHouseServiceImpl implements IXlkjHouseService
{
    @Autowired
    private XlkjHouseMapper xlkjHouseMapper;

    /**
     * 查询房屋
     *
     * @param id 房屋主键
     * @return 房屋
     */
    @Override
    public XlkjHouse selectXlkjHouseById(Long id)
    {
        return xlkjHouseMapper.selectXlkjHouseById(id);
    }

    /**
     * 查询房屋列表
     *
     * @param xlkjHouse 房屋
     * @return 房屋
     */
    @Override
    public List<XlkjHouse> selectXlkjHouseList(XlkjHouse xlkjHouse)
    {
        return xlkjHouseMapper.selectXlkjHouseList(xlkjHouse);
    }

    /**
     * 新增房屋
     *
     * @param xlkjHouse 房屋
     * @return 结果
     */
    @Override
    public int insertXlkjHouse(XlkjHouse xlkjHouse)
    {
        xlkjHouse.setCreateTime(DateUtils.getNowDate());
        return xlkjHouseMapper.insertXlkjHouse(xlkjHouse);
    }

    /**
     * 修改房屋
     *
     * @param xlkjHouse 房屋
     * @return 结果
     */
    @Override
    public int updateXlkjHouse(XlkjHouse xlkjHouse)
    {
        xlkjHouse.setUpdateTime(DateUtils.getNowDate());
        return xlkjHouseMapper.updateXlkjHouse(xlkjHouse);
    }

    /**
     * 批量删除房屋
     *
     * @param ids 需要删除的房屋主键
     * @return 结果
     */
    @Override
    public int deleteXlkjHouseByIds(Long[] ids)
    {
        return xlkjHouseMapper.deleteXlkjHouseByIds(ids);
    }

    /**
     * 删除房屋信息
     *
     * @param id 房屋主键
     * @return 结果
     */
    @Override
    public int deleteXlkjHouseById(Long id)
    {
        return xlkjHouseMapper.deleteXlkjHouseById(id);
    }

    /**
     * 房屋数据
     *
     * @param details
     */
    @Override
    public String importDevice(List<XlkjHouse> details) {
        if (StringUtils.isNull(details) || details.size() == 0){
            throw new CustomException("导入的房屋数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (XlkjHouse user : details){
            try{
                this.insertXlkjHouse(user);
                successNum++;
                successMsg.append("导入成功");
            }catch (Exception e){
                failureNum++;
                String msg = "导入失败";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else{
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<XlkjHouse> selectHouseByDepartmentId(String[] departmentId) {
        return xlkjHouseMapper.selectHouseByDepartmentId(departmentId);
    }

    @Override
    public XlkjHouseSurvey selectHouseNumByDeptId(Long deptId) {
        return xlkjHouseMapper.selectHouseNumByDeptId(deptId);
    }
}
